using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework.Library.Converters;
using System.ComponentModel;
using System.Globalization;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Library;
using System.Configuration;
using Microsoft.Practices.RecipeFramework.Extensions.Converters;
using System.CodeDom.Compiler;

namespace Microsoft.Practices.RepositoryFactory.Converters
{
    /// <summary/>
    internal class ConnectionNameConverter : CodeIdentifierStringConverter
    {
        /// <summary>
        /// Converts and string from string, if the string contains spaces then are replaced by underscore
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            return base.ConvertFrom(context, culture, value);
        }

        /// <summary>
        /// Converts and string from string, if the string contains spaces then are replaced by underscore
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <param name="destinationType"></param>
        /// <returns></returns>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            return base.ConvertTo(context, culture, value, destinationType);
        }

        /// <summary>
        /// Validates that the received value is a code identifier
        /// for the language that is using
        /// </summary>
        /// <param name="context"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool IsValid(ITypeDescriptorContext context, object value)
        {
            string stringValue = value as string;
            if(stringValue != null)
            {
                DTE dte = (DTE)context.GetService(typeof(DTE));

                if(dte.SelectedItems.Count == 1)
                {
                    Project project = null;
                    SelectedItem selection = dte.SelectedItems.Item(1);

                    if(selection.ProjectItem != null)
                    {
                        project =  selection.ProjectItem.ContainingProject;
                    }
                    else if(selection.Project != null)
                    {
                        project = selection.Project;
                    }

                    if(project != null)
                    {
                        ConnectionStringsSection section = ConnectionSettingsInfo.GetSection((EnvDTE.DTE)context.GetService(typeof(EnvDTE.DTE)));

                        if(section != null)
                        {
                            foreach(ConnectionStringSettings cs in section.ConnectionStrings)
                            {
                                if(cs.Name.Equals(stringValue, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    return false;
                                }
                            }
                        }

                        value = stringValue.Replace(' ', '_');

                        return CodeGenerator.IsValidLanguageIndependentIdentifier(stringValue);
                    }
                }
            }

            return false;
        }
    }
}